package com.gryffonx.NameGenerators;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.utils.StringUtils;
import com.gryffonx.utils.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;

/**
 * **************************************************************************
 * Application:
 * Author: Erik Stell
 * Date: Sep 9, 2004
 * Time: 9:39:47 AM
 * Process:
 * **************************************************************************
 */
public class HumanNameGenerator {
    private static final File fileToBeParsed = new File("src/com/gryffonx/xml/humans.xml");
    private static String gender = "";
    private static final SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;

    /**
     * @return
     */
    private static String getGender() {
        return gender;
    }

    /**
     * @param gender
     */
    private static void setGender(String gender) {
        HumanNameGenerator.gender = gender;
    }

    public static String generateSurname(){
        return "Stonebow";
    }

    public static String generateName(String gender, int howMany, boolean addTitle, boolean addSurname) throws Exception {
        if (howMany <= 0) {
            howMany = 1;
        }

        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        setGender(gender);
        StringBuffer retVal = new StringBuffer();

        for (int i = 0; i < howMany; i++) {
            StringBuffer finishedName = new StringBuffer();

            try {
                finishedName.append(getPrefix(determineGroup()));
            } catch (Exception e) {
                e.printStackTrace();
            }

            retVal.append(StringUtils.capitalize(finishedName.toString()));
            if (addTitle){
                retVal.append(TitleGenerator.generateName(1));
            }
            if (addSurname){
                retVal.append(" ").append(generateSurname());
            }

            //  retVal.append("\n");
        }
        return retVal.toString();
    }

    public static String generateName() {
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        setGender(gender);
        StringBuffer retVal = new StringBuffer();
        StringBuffer finishedName = new StringBuffer();

        try {
            finishedName.append(getPrefix(determineGroup()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        retVal.append(StringUtils.capitalize(finishedName.toString()));
        return retVal.toString();
    }


    private static String getPrefix(int group) {
        String retVal = "";

        if (group == 1) {
            retVal = generateGroup1();
        } else if (group == 2) {
            retVal = generateGroup2();
        }
        return retVal;
    }

    /**
     * Figure out randomly how many syllables the name is going to have.
     *
     * @return
     */
    private static int determineGroup() {
        return StaticDiceRoller.rolldSomething(2, 1);
    }

    /**
     * PREFIX1 + SUFFIX1
     *
     * @return
     */
    private static String generateGroup1() {
        StringBuffer retVal = new StringBuffer();

        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        //retVal.append(XMLUtils.getNodeValue("middle", xmlDoc, 1));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));


        return retVal.toString();
    }


    /**
     * PREFIX1 + MIDDLE1 + SUFFIX1
     * i.e. ab + a + berial
     *
     * @return
     */
    private static String generateGroup2() {
        StringBuffer retVal = new StringBuffer();

        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        retVal.append(XMLUtils.getNodeValue("suffix", xmlDoc, 1));

        return retVal.toString();
    }







}
